//
//  InformationViewController.m
//  Ting
//
//  Created by 安静的美男子 on 14/11/12.
//  Copyright (c) 2014年 安静的美男子. All rights reserved.
//

#import "InformationViewController.h"
#import "BozhuMainViewController.h"
@interface InformationViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation InformationViewController

- (void)viewDidLoad {
    self.DataSource = [NSMutableArray array];
    self.view.backgroundColor = [UIColor blackColor];
    
    [super viewDidLoad];
    
    [self getData];
    
    [self creatTable];
    
    self.mbview = [[MBProgressHUD alloc] initWithView:self.view];
    self.mbview.labelText = @"正在努力加载中...";
    [self.mbview setMode:MBProgressHUDModeIndeterminate];
    [self.mbview show:YES];
    [self.view addSubview:self.mbview];

    
    [self CreatTabbar];
    
    // Do any additional setup after loading the view.
}

-(void)creatTable
{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height - 64 - 44) style:UITableViewStyleGrouped];
    _table.delegate = self;
    _table.dataSource = self;
    
    [self.view addSubview:_table];
    
}
-(void)getData
{
    
    GetData *data = [[GetData alloc]init];
    [data getDataURL:[NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_user_index?device=iPhone&page=1"] dic:nil block:^(id result) {
        NSArray *arr = [result objectForKey:@"list"];
        for (NSDictionary *dic in arr) {
            
            boZhuModel *model = [[boZhuModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.DataSource addObject:model];

        }
        NSArray *arr1 = [[result objectForKey:@"recommendBozhu"] objectForKey:@"list"];
        
        boZhuModel *lastModel = [[boZhuModel alloc]init];
        
        lastModel.name = @"all";
        lastModel.title = @"新晋播主";
        lastModel.MyId = @"0";
        lastModel.list = arr1;
        [_DataSource addObject:lastModel];
        
        [self.mbview removeFromSuperview];

        [_table reloadData];
        
    }];
   
}
-(void)CreatTabbar
{
  
    UIView *black = [[UIView alloc]initWithFrame:CGRectMake(64 * 2, 568 - 64, 64, 64)];
    [self.view addSubview:black];
    black.layer.cornerRadius = 32;
    black.clipsToBounds = YES;
    
    black.backgroundColor = [UIColor blackColor];
    
    self.play = [UIButton buttonWithType:UIButtonTypeCustom];
    
    self.play.frame = CGRectMake(0, 0, 60, 60);
    self.play.center = black.center;
    
    
    [self.view addSubview:self.play];
    
    self.play.layer.cornerRadius = 30;
    [self.view bringSubviewToFront:self.play];
    self.play.clipsToBounds = YES;
    [self.play addTarget:self action:@selector(GotoPlay) forControlEvents:UIControlEventTouchUpInside];
    
    [self.play sd_setBackgroundImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.Playmodel.coverLarge]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"123.png"]];
    
    
    
}

-(void)viewWillAppear:(BOOL)animated
{
    AudioStreamerManager *audioManager = [AudioStreamerManager shareInstanceManager];
    _Playmodel = audioManager.Mymodel;
    
    
    if (audioManager.flag) {
        
        if (!_Timer) {
            //            self.Timer = [NSTimer timerWithTimeInterval:0.01 target:self selector:@selector(zhuan) userInfo:nil repeats:YES];
            
            self.Timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(zhuan) userInfo:nil repeats:YES];
        }
        
        //将定时器加入循环
        //循环用于系统页面响应,定时器和进程
        //                [[NSRunLoop currentRunLoop] addTimer:_Timer forMode:NSDefaultRunLoopMode];
        
        
        [_Timer setFireDate:[NSDate distantPast]];
        
        
        
        
    }
    else
    {
        
        [_Timer setFireDate:[NSDate distantFuture]];
        
        //        if ([_Timer isValid]) {
        //            永久停止
        //            [self.Timer invalidate];
        //            _Timer =nil;
        //        }
        
        
    }
    
}
-(void)zhuan
{
    self.play.transform = CGAffineTransformRotate(self.play.transform,2 * M_PI / 60.0 /100);
    //    if (_model.coverLarge) {
    [self.play sd_setBackgroundImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.Playmodel.coverLarge]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"123.png"]];
    //    }
}

-(void)GotoPlay
{
    PlayViewController *vc = [[PlayViewController alloc]init];
    AudioStreamerManager *audioManager = [AudioStreamerManager shareInstanceManager];
    vc.model = audioManager.Mymodel;
    vc.DataSource = audioManager.Mydata;
    /**
     *  number不记录
     */
    
    //    vc.number = audioManager.num;
    vc.num = [NSString stringWithFormat:@"%ld",audioManager.num];
    
    
    [self presentViewController:vc animated:YES completion:nil];
    
}
-(void)viewDidDisappear:(BOOL)animated
{
    [self.Timer invalidate];
    _Timer =nil;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_DataSource.count !=0) {
        return 1;
    }else
    {
    return  0;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *bozhu = @"bozhu1";
    BoZhuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:bozhu];
    if (!cell) {
        cell = [[BoZhuTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:bozhu];
    }
    
  
        [cell setModel:_DataSource[indexPath.section]];
    [cell.more setTag:indexPath.section + 1000];
    
    [cell.more addTarget:self action:@selector(more:) forControlEvents:UIControlEventTouchUpInside];
    
    [cell.leftBt addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchUpInside];
    [cell.leftBt setTag:indexPath.section + 200];
    
    [cell.centerBt addTarget:self action:@selector(centerAction:) forControlEvents:UIControlEventTouchUpInside];
    [cell.centerBt setTag:indexPath.section + 201];

    [cell.rightBt addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
    [cell.rightBt setTag:indexPath.section + 202];

    return cell;
    
}

-(void)leftAction:(UIButton *)sender
{
    BozhuMainViewController *vc = [[BozhuMainViewController alloc]init];
    vc.Uid = [[[_DataSource[sender.tag - 200] list]objectAtIndex:0] objectForKey:@"uid"];
    [self.navigationController pushViewController:vc animated:YES];
    
}
-(void)centerAction:(UIButton *)sender
{
    BozhuMainViewController *vc = [[BozhuMainViewController alloc]init];
    vc.Uid = [[[_DataSource[sender.tag - 201] list]objectAtIndex:1] objectForKey:@"uid"];
    [self.navigationController pushViewController:vc animated:YES];
    
}-(void)rightAction:(UIButton *)sender
{
    BozhuMainViewController *vc = [[BozhuMainViewController alloc]init];
    vc.Uid = [[[_DataSource[sender.tag - 202] list]objectAtIndex:2] objectForKey:@"uid"];
    [self.navigationController pushViewController:vc animated:YES];
    
}
-(void)more:(UIButton *)sender
{
    NextBozhuViewController *vc = [[NextBozhuViewController alloc]init];
    vc.name = [_DataSource[sender.tag - 1000] name];
    vc.title = [_DataSource[sender.tag - 1000] title];
    
    [vc.navigationItem setHidesBackButton:YES];
    
    [self.navigationController pushViewController:vc animated:YES];
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _DataSource.count;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
    
}
-(void)GotoSetting
{
    SettingViewController *vc = [[SettingViewController alloc]init];
    [self.navigationController pushViewController:vc animated:NO];
    
}
-(void)GotoAlbum
{
    AlbumViewController *vc = [[AlbumViewController alloc]init];
    [vc.navigationItem setHidesBackButton:YES];
    vc.title = @"下载";
    
    [self.navigationController pushViewController:vc animated:NO];
    
}
-(void)GotoFind
{
    FindViewController *vc = [[FindViewController alloc]init];
//    [self.navigationController popToViewController:vc animated:YES];
    [vc.navigationItem setHidesBackButton:YES];
    
    [self.navigationController pushViewController:vc animated:NO];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
